package com.yingdong.handler;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.yingdong.annotation.NoCheckPlayer;
import com.yingdong.annotation.OpCode;
import com.yingdong.db.DBObject;
import com.yingdong.logic.domain.GameUser;
import com.yingdong.logic.service.OnlineService;
import com.yingdong.logic.service.SceneThreadService;
import com.yingdong.opcode.ServerOpcode;
import com.yingdong.sceneserver.inner.message.RecvGameMessage;
import com.yingdong.sceneserver.net.GameUserSession;

@Service
public class LogOffHandler {
	static Log logger = LogFactory.getLog(LogOffHandler.class);

	@NoCheckPlayer
	@OpCode(id = ServerOpcode.GATEDISCONNECT)
	public static void handleLogoff(GameUserSession session, RecvGameMessage msg) {
		synchronized (session) {
			session.setAlive(false);
			session.setActive(false);
			GameUser gameUser = OnlineService.getGameUserInSession(session);
			OnlineService.clearGameUserSession(session);
			if (gameUser != null) {
				logger.info("玩家:" + gameUser.getUsr().getUserName() + "下线");
				if (gameUser != null) {
					OnlineService.removeOnline(gameUser.getUserId());
					SceneThreadService.gameThreadPool.execute(new Runnable() {
						public void run() {
							gameUser.saveToDb();
						}
					});
				}
			}
		}
	}
}
